/**
 * @author LKQ
 * @date 2022/1/10 10:38
 * @description 单指针，先将0移到前面，然后将1以后0的后面
 */
public class FormalSolution {
    public static void main(String[] args) {
        FormalSolution formalSolution = new FormalSolution();
        int[] nums = {2,0,2,1,1,0};
        formalSolution.sortColors(nums);
    }
    public void sortColors(int[] nums) {
        int n = nums.length;
        int ptr = 0;
        for (int i = 0; i < n; ++i) {
            if (nums[i] == 0) {
                swap(nums, i, ptr);
                ++ptr;
            }
        }
        for (int i = ptr; i < n; ++i) {
            if (nums[i] == 1) {
                swap(nums, i, ptr);
                ++ptr;
            }
        }
    }
    public void swap(int[] nums, int p, int q) {
        int temp = nums[p];
        nums[p] = nums[q];
        nums[q] = temp;
    }

}
